package hanhns.test.broadcastReceiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.widget.Toast;
import android.net.*;

import hanhns.test.R;
import hanhns.test.provider_database.BNoteProvider;

public class Receiver extends BroadcastReceiver {
	private NotificationManager mManager;
	private int NOTIFY_ID;
	public static final String key = "notify_id";
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Uri noteUri = (Uri) intent.getData();
		Cursor cursor = context.getContentResolver().query(noteUri, null, null, null, null);
		
		if (cursor != null && cursor.getCount() > 0 ) {
			Toast.makeText(context, "get success new note", Toast.LENGTH_SHORT).show();
			cursor.moveToFirst();
			
			String title = cursor.getString(cursor.getColumnIndex(BNoteProvider.NOTE_TITLE_COLUMN));
			String content = cursor.getString(cursor.getColumnIndex(BNoteProvider.NOTE_CONTENT_COLUMN));
			NOTIFY_ID = cursor.getInt(cursor.getColumnIndex(BNoteProvider._ID_COLUMN));
			Toast.makeText(context, "title:" + title + ", content: " + content, Toast.LENGTH_LONG).show();
			
			mManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
			Notification notification = new Notification(R.drawable.ic_tab_artists_white,"bNote notify!", System.currentTimeMillis());
//			notification.setLatestEventInfo(context,title,content, PendingIntent.getActivity(context, 0, intent,
//					PendingIntent.FLAG_CANCEL_CURRENT));
//			
//			mManager.notify(NOTIFY_ID, notification);
			
			Intent notyfiIntent = new Intent(Intent.ACTION_EDIT, intent.getData());
			notyfiIntent.putExtra(key, NOTIFY_ID);
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notyfiIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
			notification.setLatestEventInfo(context, title, content, pendingIntent);
			mManager.notify(NOTIFY_ID, notification);
            
		} else {
			Toast.makeText(context, "get unsuccess new note", Toast.LENGTH_SHORT).show();
		}
//		Toast toast = Toast.makeText(context, "broadcast receiver data", Toast.LENGTH_SHORT);
//		toast.show();
		
	}

}
